﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BDGetQuote.Web.Models;
using BDGetQuote.Utilities;

namespace BDGetQuote.Web.Controllers
{
    public class LoginController : Controller
    {
        private IUserInfoRepository _userRepository;
        public LoginController() : this(new UserInfoRepository()) { }
        public LoginController(IUserInfoRepository userRepository)
        {
            _userRepository = userRepository;
        }
        //
        // GET: /Login/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(UserInfo model)
        {
            if (!ModelState.IsValid)
                return View();
            var _message= string.Empty;
            if (_userRepository.CheckLogin(model.UserId.Trim(),model.Password.Trim(),out _message))
            {
                Session[Constants.SessionUser] = _userRepository.GetByUserId(model.UserId.Trim());
                return RedirectToAction("Index", "Manufacturer");
            }
            else
            {
                ViewBag.ErrorMessage = _message;
                return View();
            }
        }
        public ActionResult Logout()
        {
            Session[Constants.SessionUser] = null;
            return RedirectToAction("Index","Login");
        }
    }
}
